package com.tengju.support.infrastructure.repository.article;

import com.tengju.support.domain.model.article.Article;
import com.tengju.support.domain.model.article.ArticleId;
import com.tengju.support.domain.model.article.ArticleRepository;
import com.tengju.support.domain.model.article.ArticleType;
import com.tengju.support.domain.shared.IdObject;
import com.tengju.support.infrastructure.persistence.mybatis.dataobject.ArticleDO;
import com.tengju.support.infrastructure.persistence.mybatis.mapper.tengju.custom.ArticleMapper;
import com.tengju.support.infrastructure.repository.article.convert.ArticleConvert;
import com.tengju.support.infrastructure.shared.InfraException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class ArticleRepositoryImpl implements ArticleRepository {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public ArticleId insert(Article article) {

        ArticleDO articleDO = ArticleConvert.convertToDO(article);
        articleDO.setCreateTime(LocalDateTime.now());
        articleDO.setUpdateTime(LocalDateTime.now());
        articleMapper.insert(articleDO);

        return new ArticleId(articleDO.getArticleId());
    }

    @Override
    public Article get(ArticleId articleId) {

        ArticleDO articleDO = articleMapper.getById(articleId.getValue());
        return ArticleConvert.convertToModel(articleDO);

    }

    @Override
    public int edit(Article article) {
        ArticleDO articleDO = ArticleConvert.convertToDO(article);
        int update = articleMapper.updateContext(articleDO);
        if (update != 1) {
            throw new InfraException("update article context error");
        }
        return update;
    }

    @Override
    public int del(ArticleId articleId) {
        int delete = articleMapper.delete(articleId.getValue());
        if (delete != 1) {
            throw new InfraException("delete article error");
        }
        return delete;
    }

    @Override
    public int count(ArticleId articleId, String title, ArticleType articleType) {
        Long id = Optional.ofNullable(articleId).map(IdObject::getValue).orElse(null);
        Integer type = Optional.ofNullable(articleType).map(ArticleType::getVal).orElse(null);
        return articleMapper.count(id, title, type);
    }

    @Override
    public List<Article> queryByPage(ArticleId articleId, String title, ArticleType articleType, Integer offset, Integer pageSize) {
        Long id = Optional.ofNullable(articleId).map(IdObject::getValue).orElse(null);
        Integer type = Optional.ofNullable(articleType).map(ArticleType::getVal).orElse(null);
        List<ArticleDO> articleDOList = articleMapper.queryByPage(id, title, type, offset, pageSize);
        return articleDOList.stream().map(ArticleConvert::convertToModel).collect(Collectors.toList());
    }

    @Override
    public int updateViewCnt(ArticleId articleId) {
        return articleMapper.updateViewCnt(articleId.getValue());
    }
}
